package com.google.code.p.odbcconnectionchecker;

import java.awt.EventQueue;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;

import com.google.code.p.odbcconnectionchecker.UI.ConnectionWindow;

public class ConnectionCheckerPopupMenu extends PopupMenu implements Serializable {
	
	private static final long serialVersionUID = 4L;
	
	/*
	 * User's defined JDBC Connections. The ArrayList is persisted through serialization.
	 */
	private ArrayList<ConnectionMenu> connectionMenu = new ArrayList<ConnectionMenu>();
	
	/**
	 *  Constructor populates the PopupMenu with default Menu Items 
	 */
	public ConnectionCheckerPopupMenu () {
		this.add(getNewConnectionMenuItem());
		this.add(getRefreshMenuItem());
		this.add(getExitMenuItem());
		
		this.addSeparator();
		
		ConnectionWindow.setPopupMenu(this);
		ConnectionMenu.setPopupMenu(this);
	}
	
	/*
	 * Default MenuItem: New Connection
	 */
	private MenuItem getNewConnectionMenuItem(){
		MenuItem newConnectionMenuItem = new MenuItem("New Connection");
		newConnectionMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							new ConnectionWindow();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});	
			} 
		});
		return newConnectionMenuItem;
	}
	
	/*
	 * Default MenuItem: Refresh All Connections' Status
	 */
	private MenuItem getRefreshMenuItem() { 
		MenuItem refreshItem = new MenuItem("Check All Connection Status");
		refreshItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (ConnectionMenu cm : connectionMenu) { 
					cm.forceCheckConnection();
				}
			}
		});
		return refreshItem;
	}
	
	/*
	 * Default MenuItem:
	 * Serialize the currenct Class
	 * Close the application
	 */
	private MenuItem getExitMenuItem() { 
		MenuItem exitMenuItem = new MenuItem("Exit");
		//Save action responsible for serialization
		ConnectionCheckerSaveConfiguration saveAction = new ConnectionCheckerSaveConfiguration();
		saveAction.setPopupMenu(this);
		exitMenuItem.addActionListener(saveAction);
		return exitMenuItem;
	}
	
	/**
	 * Adds a new connection to the popup menu
	 * @param connection A ConnectionMenu object
	 */
	public void newConnection(ConnectionMenu connection) {
		connectionMenu.add(connection);
		this.add(connection);
	}
	
	/**
	 * Deletes a connection from the popup menu
	 * @param connection A ConnectionMenu object
	 */
	public void deleteConnection(ConnectionMenu connection) {
		this.remove(connection);
		connectionMenu.remove(connection);
	}

	/**
	 * 
	 * @return ArrayList of the user defined connections 
	 */
	public ArrayList<ConnectionMenu> getConnectionMenu() {
		return connectionMenu;
	}

	/**
	 * Import existing connections defined by the user
	 * @param connectionMenu
	 */
	public void setConnectionMenu(ArrayList<ConnectionMenu> connectionMenu) {
		this.connectionMenu = connectionMenu;
		for (ConnectionMenu cm : this.connectionMenu) {
			this.add(cm);
		}
	}
}
